package com.fling.abcde.activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.widget.Toast;

import com.fling.abcde.BaseApplication;
import com.fling.abcde.R;

import com.fling.abcde.base.BaseActivity;
import com.fling.abcde.fragment.Home1Fragment;
import com.fling.abcde.fragment.Home2Fragment;
import com.fling.abcde.fragment.HomeFragment;
import com.lib.common.adapter.FragmentBaseAdapter;
import com.lib.common.dialog_fragment.LoginDialogFragment;
import com.lib.common.view.ImageTextView;
import com.lib.utils.ToastUtils;

import java.util.ArrayList;

/**
 * @author FengLing
 * Created on 2017-07-27 21:42
 */
public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    public static MainActivity sMainActivity;
    private long mExitTime;
    private ViewPager mMainViewPager;
    private ArrayList<Fragment> mFragList;
    private ArrayList<ImageTextView> mBtnList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sMainActivity = this;
        setActionBarLeftVisible(false);
        baseBinding.baseActionBarRight.setText(ImageTextView.TEXT_RIGHT, "登录");
        setContentView(R.layout.activity_main);

        mFragList = new ArrayList<>();
        mFragList.add(new HomeFragment());
        mFragList.add(new Home1Fragment());
        mFragList.add(new Home2Fragment());

        mBtnList = new ArrayList<>();
        mBtnList.add(findViewById(R.id.home));
        mBtnList.add(findViewById(R.id.home1));
        mBtnList.add(findViewById(R.id.home2));

        mMainViewPager = findViewById(R.id.main_viewPager);
        mMainViewPager.setAdapter(new FragmentBaseAdapter(mFragList, getSupportFragmentManager()));
        mMainViewPager.addOnPageChangeListener(this);

        for (ImageTextView view : mBtnList) {
            view.setOnClickListener(this);
        }
    }

    @Override
    protected void onBaseRightClicked() {

        new LoginDialogFragment().show(this, false);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        for (int i = 0; i < mBtnList.size(); i++) {
            ImageTextView view = mBtnList.get(i);
            view.setChecked(i == position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.home:

                mMainViewPager.setCurrentItem(0, false);
                break;
            case R.id.home1:

                mMainViewPager.setCurrentItem(1, false);
                break;
            case R.id.home2:

                mMainViewPager.setCurrentItem(2, false);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            mExitTime = System.currentTimeMillis();
            ToastUtils.makeText(this, "再次点击,即可退出程序", Toast.LENGTH_SHORT).show();
        } else {
            super.onBackPressed();
            BaseApplication.safeExitApp();
        }
    }

    @Override
    protected void onDestroy() {

        sMainActivity = null;
        super.onDestroy();
    }
}